package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.DieChicken;
import com.example.demo.bean.InventoryDie;
import com.example.demo.mapper.DieChickenMapper;
import com.example.demo.service.InsertData;
import com.example.demo.util.GetData;
import com.example.demo.util.LoginAndToken;
import com.sun.javafx.fxml.builder.URLBuilder;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.net.URIBuilder;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


@Service
public class InsertDataImpl implements InsertData {

    @Autowired
    DieChickenMapper dieChickenMapper;
     public  int  a = 0;

    @Override
    public int insertDieChicken(String date, String name,String isAudit,String isSend,Integer orgId) throws URISyntaxException, IOException, ParseException {

        String url = "http://ny.shuanghui.net:9181/breed/bi/product/accident/list";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.setParameter("startDate",date);
        // 获取前一天的日期
        LocalDate yesterday = LocalDate.now().plusDays(-1);

        // 格式化输出（可选）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = yesterday.format(formatter);
        uriBuilder.setParameter("endDate", formattedDate);
        uriBuilder.setParameter("orgId", orgId.toString());
        //是否审核
        uriBuilder.setParameter("isAudit", isAudit);
        //是否上传
        uriBuilder.setParameter("isSend", isSend);
        uriBuilder.setParameter("isAiDiffAdjust", "N");
        uriBuilder.setParameter("pageNum", "1");
        uriBuilder.setParameter("pageSize", "10000");
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        httpGet.setHeader("Token", LoginAndToken.token);
        CloseableHttpResponse response = LoginAndToken.httpClient.execute(httpGet);
        JSONObject data = GetData.getData(response);

        String page = data.getString("page");
        JSONObject pagesObject = JSON.parseObject(page);
        String list = pagesObject.getString("list");
        JSONArray jsonArray = new JSONArray(list);

        for (int i = 0; i < jsonArray.length(); i++) {
            DieChicken dieChicken = new DieChicken();
            org.json.JSONObject list1 = jsonArray.getJSONObject(i);
            //栋舍名称
            String houseName = list1.getString("houseName");
            //栋舍对应的死淘总数
            Integer invQty = (Integer) list1.get("invQty");
            dieChicken.setOrgName(name);//机构名称
            dieChicken.setOrgId(orgId);//机构代码
            dieChicken.setDate((String) list1.get("createDateTime"));//设置日期
            dieChicken.setDeadChickenCount((Integer) list1.get("accidentNum"));
            dieChicken.setCulledChickenCount(list1.optString("weedNum"));
            dieChicken.setTotalMortalityCount(invQty);
            dieChicken.setHoseName(houseName);
            dieChickenMapper.insertDieChickenMapper(dieChicken);
            a++;//记录插入多少条数据
        }

        return a;

    }
}
